package com.zy.yygh.cmn.controller;

import com.zy.yygh.cmn.service.DictService;
import com.zy.yygh.common.result.Result;
import com.zy.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(value = "数据字典")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;

    //导出数据字典接口
    @GetMapping("exportData")
    @ApiOperation("导出数据操作")
    public void exportDict(HttpServletResponse response){
        dictService.exportDicdata(response);
    }

    //导入数据字典接口
    @PostMapping ("importData")
    @ApiOperation("导出数据操作")
    public Result exportDict(MultipartFile file){
        dictService.importDictData(file);
        return Result.ok();
    }

//    根據dictCode 獲取下級節點
    @ApiOperation(value = "根據dictCode获取下级子节点")
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable("dictCode") String dictCode){
        List<Dict> list=dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }


//    根据id查询子数据
    @GetMapping("findChildData/{id}")
    @ApiOperation("子数据查询")
    public Result  findChildData(@PathVariable Long id){
        List<Dict> list=dictService.findChildData(id);
        return Result.ok(list);
    }

//    根据dictCode和value值查询
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable("dictCode") String dictCode,
                          @PathVariable("value") String value){
        String dictName=dictService.getDictName(dictCode,value);
        return dictName;
    }

//    根据value值查询
    @GetMapping("getName/{value}")
    public String getName(@PathVariable("value") String value){
        String dictName=dictService.getDictName("",value);
        return dictName;
    }

}

